<?php
class duoduo {

	public $link;
	public $field_arr;

	function __construct($dbserver = '', $dbuser = '', $dbpass = '', $dbname = '', $BIAOTOU = '') {
		$this->link = mysql_connect($dbserver, $dbuser, $dbpass);
		mysql_select_db($dbname);
		$this->query("set names utf8", $this->link);
		$this->BIAOTOU = $BIAOTOU;
		
		/*$url='localhost';
		$cur_url=$_SERVER['HTTP_HOST'];
		if($this->check_url($url, $cur_url)==0){
		    echo $this->alert('请购买正式授权！',"http://fanli.goodyo.com");
			exit;
		}*/
	}

	function __destruct() {
		mysql_close($this->link);
	}

	function query($sql) {
		return mysql_query($sql);
	}

	function fetch_array($query, $result_type = MYSQL_ASSOC) {
		return mysql_fetch_array($query, $result_type);
	}

	function error() {
		return mysql_error();
	}

	function sel_sql($table, $sel_field, $where, $alert = 0) {
		$sql = "select $sel_field from " . $this->BIAOTOU . $table . " where $where limit 1";
		if ($alert == 1) {
			echo $sql;
		}
		$query = $this->query($sql);
		if ($query) {
			while ($row = $this->fetch_array($query)) {
				if (strpos($sel_field, ",") or $sel_field == "*") {
					$arr = $row;
				} else {
					$arr = $row[$sel_field];
				}
			}
		}
		return $arr;
	}

	function update_sql($table, $field_arr, $where, $xs, $alert = 0) { //$xs 0表示更新 1表示同加  2表示同减,$alert默认为0,1表示打印sql语句,2表示先执行在打印
		foreach ($field_arr as $k => $v) {
			if ($xs == 0) {
				$sql = "`$k`='$v'," . $sql;
			}
			elseif ($xs == 1) {
				$sql = "`$k`=`$k`+'$v'," . $sql;
			}
			elseif ($xs == 2) {
				$sql = "`$k`=`$k`-'$v'," . $sql;
			}
		}
		$sql = substr($sql, 0, strlen($sql) - 1);
		$sql = "update $this->BIAOTOU$table set $sql where $where;";
		if ($alert == 0) {
			$this->query($sql);
		}
		elseif ($alert == 1) {
			echo $sql;
		}
		elseif ($alert == 2) {
			$this->query($sql);
			echo $sql;
		}
	}
	


	function insert_one_sql($table, $field_arr, $alert = 0) {
		$field = "";
		$values = "";
		foreach ($field_arr as $k => $v) {
			$field = "`" . $k . "`," . $field;
			$values = "'" . $v . "'," . $values;
		}
		$field = substr($field, 0, strlen($field) - 1);
		$values = substr($values, 0, strlen($values) - 1);
		$sql = "insert into $this->BIAOTOU$table($field) values ($values);";
		$query = $this->query($sql);
		if ($alert == 0) {
			if ($query)
				return mysql_insert_id();
			else
				return $this->error();
		}
		elseif ($alert == 1) {
			echo $sql . "<br/>";
		}
		elseif ($alert == 2) {
			echo $sql . "<br/>";
			if ($query)
				return mysql_insert_id();
			else
				return $this->error();
		}

	}
	
	function replace_into_sql($table, $field_arr, $alert = 0) {
		$field = "";
		$values = "";
		foreach ($field_arr as $k => $v) {
			$field = "`" . $k . "`," . $field;
			$values = "'" . $v . "'," . $values;
		}
		$field = substr($field, 0, strlen($field) - 1);
		$values = substr($values, 0, strlen($values) - 1);
		$sql = "replace into $this->BIAOTOU$table($field) values ($values);";
		$query = $this->query($sql);
		if ($alert == 0) {
			if ($query)
				return mysql_insert_id();
			else
				return $this->error();
		}
		elseif ($alert == 1) {
			echo $sql . "<br/>";
		}
		elseif ($alert == 2) {
			echo $sql;
			if ($query)
				return mysql_insert_id();
			else
				return $this->error();
		}

	}

	function sel_arr_sql($table, $sel_field, $where, $alert = 0) {
		global $BIAOTOU;
		$arr = array ();
		$sql = "select $sel_field from $BIAOTOU$table where $where ";
		if ($alert == 1) {
			echo $sql;
		}
		$query = $this->query($sql);
		if ($query) {
			while ($row = $this->fetch_array($query)) {
				$arr[] = $row;
			}
		}
		return $arr;
	}

	function sqltoarr($sql,$x=0) {
		$query = $this->query($sql);
		while ($row = $this->fetch_array($query)) {
			$arr[] = $row;
		}
		if($x==0){
		    return $arr;
		}
		else{
		    return $arr[0];
		}
		
	}
	
	function sql_count($table,$count_field,$where){
	    $sql="select count($count_field) as num from ".$this->BIAOTOU.$table." where $where";
	    $query = $this->query($sql);
	    $row=$this->fetch_array($query);
	    return $row['num']?$row['num']:0;
    }

	//////////////////////////////////////////////////////////////////////////////////////////

	function arr_diff($arr1,$arr,$xs=1){ //xs=0，arr是键值  xs=1，arr是键名
	    if($xs==1){
		    foreach($arr as $k=>$v){
			    $arr1[$v]='3645dfgsdf78ufasdgasdt345rsdfsdf';
			    $arr[$v]='3645dfgsdf78ufasdgasdt345rsdfsdf';
		    }
		    $arr = array_diff($arr1, array('3645dfgsdf78ufasdgasdt345rsdfsdf'));
	    }
	    else{
		    $arr = array_diff($arr1, $arr);
	    }
        return $arr;
    }
	
	//下拉表单 $array:数组  $name:select的name和id  $id:被选中选项的id  $interval:颜色间隔
    function select($array,$name,$id=10000,$interval=5){
	    $i=0;
	    echo "<select name='$name' id='$name'>";
	    foreach($array as $key=>$val){
		    if($id==$key) $select='selected';
		    else $select='';
		    if($i!=0){
			    if((floor(($i-1)/$interval))%2!=0) $bg="#dfefd7";
		        else $bg="";
		    }
		    echo "<option value='$key' $select style='background:$bg'>$val</option>";
		    $i++;
	    }
	    echo "</select>";
    }

	////////////////////////////////////////////////////////////////////////////////////////////
	
	function alert($word,$url=''){
	    if($url==''){
		    echo "<script>alert('".$word."');history.go(-1);</script>";
	    }
	    else{
	        echo "<script>alert('".$word."');window.location='".$url."';</script>";
	    }
    }
	
	function utf_substr($str,$len){
        for($i=0;$i<$len;$i++){
            $temp_str=substr($str,0,1);
            if(ord($temp_str) > 127){
                $i++;
                if($i<$len){
                    $new_str[]=substr($str,0,3);
                    $str=substr($str,3);
                }
            }
            else {
                $new_str[]=substr($str,0,1);
                $str=substr($str,1);
            }
        }
        return join($new_str);
    }
	
	function make_arr_cache($arr, $name, $dir) {
		$data = "<?php\n return " . var_export($arr, true) . ";\n?>";
		file_put_contents(DDROOT . CACHE . $dir . $name . '.php', $data);
	}
	
	function record($val, $filename = 're.txt') {
		$fp = fopen($filename, 'a');
		fwrite($fp, $val . "\n\r");
		fclose($fp);
	}
}
?>